from app import db

import time

class User(db.Model):
    __table_args__ = (db.UniqueConstraint('country_code', 'mobile', name = 'country_code_mobile_unique'),
                      {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8',})

    user_id = db.Column(db.Integer, primary_key = True)
    create_time = db.Column(db.Integer, default = int(time.time()))
    update_time = db.Column(db.Integer, default = int(time.time()), onupdate = int(time.time()))
    register_ip = db.Column(db.String(32), nullable = False, default = '')
    name = db.Column(db.String(32), nullable = False, default = '')
    email = db.Column(db.String(64), nullable = True, unique = True, default = None)
    has_email_verified = db.Column(db.Boolean, default = False)
    email_update_time = db.Column(db.Integer)
    country_code = db.Column(db.String(8), nullable = True, default = None)
    mobile = db.Column(db.String(32), nullable = True, default = None)
    mobile_update_time = db.Column(db.Integer)
    login_password_hash = db.Column(db.String(128), nullable = False, default = '')
    login_password_update_time = db.Column(db.Integer)
    trade_password_hash = db.Column(db.String(128), nullable = True, default = '')
    trade_password_update_time = db.Column(db.Integer)
    totp_auth_key = db.Column(db.String(128), nullable = False, default = '')
    totp_auth_update_time = db.Column(db.Integer)
    trade_password_frequency = db.Column(db.String(32), nullable = False, default = '')
    login_password_level = db.Column(db.String(32), nullable = False)
    lang = db.Column(db.String(32), nullable = False, default = '')
    timezone = db.Column(db.SmallInteger, nullable = False, default = -8)
    user_label                     = db.Column(db.String(64), nullable=True)
    has_join_leverage              = db.Column(db.Boolean, nullable=False, default=False)

    def __repr__(self):
        return '<User(%s:%s:%s:%s:%s)>' % (
            self.name, self.email, self.country_code, self.mobile, self.login_password_level
        )



class JuniorIdentity(db.Model):
    __table_args__ = (db.UniqueConstraint('id_card', 'id_type', 'identity_type', name='id_card_id_type_identity_type_unique'),
    {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'})

    junior_identity_id = db.Column(db.Integer, primary_key=True)
    create_time = db.Column(db.Integer, default='')
    update_time = db.Column(db.Integer, default='', onupdate='')
    user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), unique=True)
    country_id = db.Column(db.Integer, db.ForeignKey('country.country_id'))
    first_name = db.Column(db.String(64), nullable=False)
    middle_name = db.Column(db.String(64), nullable=False)
    last_name = db.Column(db.String(64), nullable=False)
    id_card = db.Column(db.String(128), nullable=False, index=True)
    id_address = db.Column(db.String(256), nullable=False, default='')
    birthday = db.Column(db.Date, nullable=True)
    log = db.Column(db.String(512), nullable=False, default='')
    sex = db.Column(db.String(32), nullable=False, default='M')
    id_type = db.Column(db.String(32), nullable=False)
    identity_type = db.Column(db.String(32), nullable=False)
    status = db.Column(db.String(32), nullable=False, index=True)
    user = db.relationship('User', backref=db.backref('junior_identity', lazy='dynamic'), uselist=False)


class BalanceHistory(object):
    _mapper = {}

    @staticmethod
    def history_model(user_id):
        table_index = user_id % 100
        class_name = str('BalanceHistory_%d' % table_index)

        ModelClass = BalanceHistory._mapper.get(class_name, None)

        if ModelClass is None:
            ModelClass = type(class_name, (db.Model,), {
                '__module__': __name__,
                '__name__': class_name,
                '__tablename__': 'balance_history_%d' % table_index,
                '__bind_key__': 'db2',

                'id': db.Column(db.Integer, primary_key=True),
                'time': db.Column('time', db.Integer),
                'user_id': db.Column(db.Integer),
                'asset': db.Column(db.String),
                'business': db.Column(db.String),
                'change': db.Column(db.Numeric),
                'balance': db.Column(db.Numeric),
                'detail': db.Column(db.String),

            })
            BalanceHistory._mapper[class_name] = ModelClass

        cls = ModelClass()
        cls.user_id = user_id
        return cls